package cn.hloger.spider.utils;

import cn.hloger.spider.common.bean.IdValueVo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

;

/**
 * TODO
 *
 * @author huanglong
 * @Description TODO
 * @data 2022/9/13
 */
public class PatternUtils {

    //用e开团面没有数字,且后
    public static Pattern nomandPattern = Pattern.compile("^([e])\\s*(?!\\d*\\s)(.*)");

    //匹配字符串中第一个e 然后后面是数字 +空格的字符串
    public static Pattern pattern = Pattern.compile("^([e])(\\d+)\\s(.*)");

    public static Pattern rolePattern = Pattern.compile("^\\s*角色设置\\s*(.*)$");


    public static IdValueVo getByContent(String content) {
        IdValueVo idValueVo = new IdValueVo();
        Matcher matcher = nomandPattern.matcher(content);
        if (matcher.find()) {
            handleRoleAndMessage(idValueVo, matcher.group(2));
            return idValueVo;
        }
        Matcher matcherMath = pattern.matcher(content);
        if (matcherMath.find()) {
            idValueVo.setId(Integer.valueOf(matcherMath.group(2)));
            handleRoleAndMessage(idValueVo, matcherMath.group(3));
            return idValueVo;
        }
        return null;
    }

    private static void handleRoleAndMessage(IdValueVo idValueVo, String message) {
        Matcher rolePatternMatcher = rolePattern.matcher(message);
        if (rolePatternMatcher.find()) {
            idValueVo.setContent(rolePatternMatcher.group(1));
            idValueVo.setRole(rolePatternMatcher.group(1));
        } else {
            idValueVo.setContent(message);
        }
    }

    public static void main(String[] args) {
        String s = "e1 角色设置我刚才说了数字是几";

        Matcher matcher = nomandPattern.matcher(s);
        if (matcher.find()) {
            System.out.println("新建的对话");
            String text0 = matcher.group(0);
            String text1 = matcher.group(1);
            String text2 = matcher.group(2);
            System.out.println("0.全部:" + text0); // 输出 "e"
            System.out.println("1.标识:" + text1); // 输出 "我是谁"
            System.out.println("2.内容:" + text2); // 输出 "我是谁"

            Matcher rolePatternMatcher = rolePattern.matcher(text2);
            if (rolePatternMatcher.find()) {
                System.out.println("3.有角色:" + rolePatternMatcher.group(1));
            } else {
                System.out.println("3.无角色");
            }
        }
        System.out.println("======================="); // 输出 "我是谁"
        Matcher matcher2 = pattern.matcher(s);
        if (matcher2.find()) {
            System.out.println("上下文的对话");
            String text0 = matcher2.group(0);
            String text1 = matcher2.group(1);
            String text2 = matcher2.group(2);
            String text3 = matcher2.group(3);
            System.out.println("0.全部:" + text0); // 输出 "e"
            System.out.println("1.标识:" + text1); // 输出 "e"
            System.out.println("2.编号:" + text2); // 输出 "我是谁"
            System.out.println("3.内容:" + text3); // 输出 "我是谁"
            Matcher rolePatternMatcher = rolePattern.matcher(text3);
            if (rolePatternMatcher.find()) {
                System.out.println("4.有角色:" + rolePatternMatcher.group(1));
            } else {
                System.out.println("4.无角色");
            }
        }

    }

}
